--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once
--开服消费排行

--是否在开服期间之内
function IsInOpenServerConsumeRankDay()
    local openday = System.getDaysSinceOpenServer()
    if openday >= OpenServerCfg.ConsumeRank.startDay and openday <= OpenServerCfg.ConsumeRank.endDay then
        return true
    end
    return false
end

--是否在开服活动结束后第一天
function IsInOpenServerConsumeRankEndDay()
    local openday = System.getDaysSinceOpenServer()
    if openday > OpenServerCfg.ConsumeRank.endDay and openday <= OpenServerCfg.ConsumeRank.endDay + 1 then
        return true
    end
    return false
end

-- 活动结束
function OpenServerConsumeRankEnd()
    if not IsInOpenServerConsumeRankEndDay() then
        return
    end
    local config = OpenServerCfg.ConsumeRank
    local rankPtr = Ranking.getRanking(RankList[RANK_OPENSERVER_CONSUME].strName)
    if rankPtr then
        local rankCount = Ranking.getRankItemCount(rankPtr)
        for idx = 1, rankCount do
            local item = Ranking.getItemFromIndex(rankPtr, idx - 1)
            if item then
                local myScore = Ranking.getPoint(item)    
                if myScore < config.needMinValue then
                    return
                else
                    local awardConfig = GetElemByCond(config.Awards, idx)
                    if not awardConfig then
                        return
                    end
                    local actorId = tonumber(Ranking.getId(item))
                    local title = string.format(Lang.ScriptTips.OpenServerRank023, idx)
                    local content = string.format(Lang.ScriptTips.OpenServerRank024, idx)
                    local nActorJob = tonumber(Ranking.getSub(item, 2))
                    local nActorSex = tonumber(Ranking.getSub(item, 3))
                    Awards.GiveToMailById(actorId, awardConfig.awards, title, content, LogIds.OpenServerConsumeRankLogId, Lang.LogTips.OpenServerConsumeRank, nActorJob, nActorSex)
                end
            end
        end
    end
end

-- 打包数据
function OpenServerConsumeRankSendData(sysarg)
    if not IsInOpenServerConsumeRankDay() then
        return
    end
    local config = OpenServerCfg.ConsumeRank
    local myIdx = 0
    local myScore = 0
    local rankPtr = Ranking.getRanking(RankList[RANK_OPENSERVER_CONSUME].strName)
    if rankPtr then
        local actorid = Actor.getActorId(sysarg)
        local item = Ranking.getItemPtrFromId(rankPtr, actorid)
        if item then
            myIdx = Ranking.getIndexFromPtr( item ) + 1
            myScore = Ranking.getPoint(item)    
            if myScore < config.needMinValue then
                myIdx = 0
            end
        end
    end
    local npack = DataPack.allocPacket(sysarg,LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sGetOpenServerRankData)
    if not npack then return end
    DataPack.writeByte(npack, 2)
    DataPack.writeShort(npack, myIdx)
    DataPack.writeInt(npack, myScore)

    DataPack.flush(npack) 
end

function OpenServerConsumeRankConsumeYuanBao(sysarg, value)
    if not IsInOpenServerConsumeRankDay() then
        return
    end
    if value <= 0 then
        return
    end
    local actorid = Actor.getActorId(sysarg)
    gRankMgr_AddValue(actorid, RANK_OPENSERVER_CONSUME, value)
    gRankMgr_SetRankEx(actorid, RANK_OPENSERVER_CONSUME, 2, Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION))
    gRankMgr_SetRankEx(actorid, RANK_OPENSERVER_CONSUME, 3, Actor.getIntProperty(sysarg, PROP_ACTOR_SEX))
    OpenServerConsumeRankSendData(sysarg)
end

